//#defines!
#include "FluidSimData.h"
#include "app.h" // For Debugging
extern FluidSim;

// Simple define for matrix indexing
#define IX(i,j) ((i)+(N+2)*(j))

FluidSimData::FluidSimData ( int Size ){
	// Allocate Proper Sizes of Matrices
	mSize       =  Size;
	int size	= (mSize+2)*(mSize+2);
	mU			= (float *) malloc ( size*sizeof(float) );
	mV			= (float *) malloc ( size*sizeof(float) );
	mU_prev		= (float *) malloc ( size*sizeof(float) );
	mV_prev		= (float *) malloc ( size*sizeof(float) );
	mD			= (float *) malloc ( size*sizeof(float) );	
	mD_prev		= (float *) malloc ( size*sizeof(float) );

	if ( !mU || !mV || !mU_prev || !mV_prev || !mD || !mD_prev) 
	{
		errprintf ( "Error\n" );
		return ( 0 );
	}
}

FluidSimData::~FluidSimData ()
{
	// Free simulation data
	if ( mU )			free ( mU );
	if ( mV )			free ( mV );
	if ( mU_prev )		free ( mU_prev );
	if ( mV_prev )		free ( mV_prev );
	if ( mD )			free ( mD );
	if ( mD_prev )		free ( mD_prev );

}
const float FluidSimData::VelocityX(unsigned int I, unsigned int J)
{
	return mU[IX(I,J)];
}
const float FluidSimData::VelocityY(unsigned int I, unsigned int J)
{
	return mV[IX(I,J)];
}
const float FluidSimData::VelocityX_Prev(unsigned int I, unsigned int J)
{
	return mU_prev[IX(I,J)];
}
const float FluidSimData::VelocityY_Prev(unsigned int I, unsigned int J)
{
	return mV_prev[IX(I,J)];
}
const float FluidSimData::Density(unsigned int I, unsigned int J)
{
	return mD[(IX(I,J)];
}
const float FluidSimData::Density_Prev(unsigned int I, unsigned int J)
{
	return mD_prev[(IX(I,J)];
}

void FluidSimData::ScreenToIndex(unsigned int ScreenWidth, unsigned int ScreenHeight, unsigned int X, unsigned int Y, unsigned int &I, unsigned int &J )
{
	I = (int)(( X /(float)ScreenWidth)*mSize+1);
	J = (int)(((ScreenHeight-Y)/(float)ScreenHeight)*mSize+1);
}
